import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class demo4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                //执行的任务
                int sum = 0;
                for (int i = 0; i < 1000; i++) {
                    sum++;
                }
                return sum;//有返回值
            }
        };

        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);//Thread不能传入Callable参数,只能通过FutureTask间接传参
        t.start();
        System.out.println(futureTask.get());//通过get方法获取到返回值

    }
}
